package com.tanglongan.designPattern.dp11_flyweight.demo01;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TicketFactory {

    private static final Map<String, ITicket> pool = new ConcurrentHashMap<>();

    public static ITicket queryTicket(String from, String to) {
        String key = from + "-->" + to;
        if (pool.containsKey(key)) {
            System.out.println("使用缓存：" + key);
            return pool.get(key);
        }

        System.out.println("首次使用车票，创建对象：" + key);
        TriainTicket triainTicket = new TriainTicket(from, to);
        pool.put(key, triainTicket);
        return triainTicket;
    }

}
